import { ref } from 'vue'
import { adapter, request } from '@/request'
import { promotionByGet } from '@/core/api/promotion'
import type { Promotion } from '@/core/interfaces/promotion'
import type { Pagination, RequestWrapper } from '@/core/interfaces/common'
import type { ServiceApproveStatusEnum, ServiceStatusEnum } from '@/core/enums/status'
import type { Order } from '@/core/interfaces/order'
import type { CustomerPack, ProxyPack, SellerPack } from '@/core/interfaces/base'
import type { Industry } from '@/core/interfaces/industry'

export interface PromotionQuery extends Pagination, CustomerPack {
  orderId: Order['id']
  serviceApproveStatus: ServiceApproveStatusEnum
  serviceStatus: ServiceStatusEnum
  sellerId: SellerPack['sellerId']
  proxyId: ProxyPack['proxyId']
  keyword: string
  industryCode?: Industry['code']
}

export function getPromotionListWithAll() {
  const service = adapter<RequestWrapper<{ ads: Promotion[]; totalSize: number }>>(request)
  const loading = ref(false)

  const fetch = (params: Partial<PromotionQuery>) => {
    const { url, method } = promotionByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [null, fetch, loading] as const
}

export default function getPromotionList() {
  const service = adapter<RequestWrapper<{ ads: Promotion[]; totalSize: number }>>(request)
  const loading = ref(false)
  const response = ref<Promotion[]>([])

  const fetch = (params: Partial<PromotionQuery>) => {
    const { url, method } = promotionByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data.ads
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
